package com.seeseafish.mongo.dao.jdbc;

import com.seeseafish.mongo.dao.mongo.MongodbDAO;
import com.seeseafish.mongo.domain.ApiLogDomain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chencheng on 2016/8/12.
 */
@Repository
public class ApiLogDAO {

    private static final Logger LOG = LoggerFactory.getLogger(ApiLogDAO.class);

    private static final String sql = "SELECT LOG_ID," +
            " TYPE," +
            " TX_CODE," +
            " DOCKING_SYS," +
            " TO_CHAR(REQUEST_TIME, 'YYYY-MM-DD hh24:mi:ss') AS \"REQUEST_TIME\"," +
            " REQUEST_CONTENT," +
            " TO_CHAR(RESPONSE_TIME, 'YYYY-MM-DD hh24:mi:ss') AS \"RESPONSE_TIME\"," +
            " RESPONSE_CONTENT," +
            " STATUS," +
            " REFERENCE," +
            " REQUEST_SEQ_NO," +
            " BUSI_MSG," +
            " LAST_OPERATOR_ID," +
            " LAST_OPERATOR," +
            " LAST_UPDATE_TIME" +
            " FROM T_HYD_INTERFACE_LOG" +
//            " where to_char(request_time,'YYYY-MM-DD hh24:mi:ss') >= '2016-08-12 15:51:30' " +
            " ORDER BY LOG_ID DESC";

    @Autowired
    private MongodbDAO mongodbDAO;

    /**
      * 从oracle迁移数据到mongodb
      */
    public void transferLogDataToMongo(){
        Connection conn = JDBCOperation.getConn();
        LOG.debug(sql);
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            int i = 0;
            while (rs.next()){
                ApiLogDomain apiLog = new ApiLogDomain();
                apiLog.setLogId(rs.getString("LOG_ID"));
                apiLog.setType(rs.getString("TYPE"));
                apiLog.setTxCode(rs.getString("TX_CODE"));
                apiLog.setDockingSys(rs.getString("DOCKING_SYS"));
                apiLog.setRequestTime(rs.getString("REQUEST_TIME"));
                apiLog.setRequestContent(rs.getString("REQUEST_CONTENT"));
                apiLog.setResponseTime(rs.getString("RESPONSE_TIME"));
                apiLog.setResponseContent(rs.getString("RESPONSE_CONTENT"));
                apiLog.setStatus(rs.getString("STATUS"));
                apiLog.setReference(rs.getString("REFERENCE"));
                apiLog.setRequestSeqNo(rs.getString("REQUEST_SEQ_NO"));
                apiLog.setBusiMsg(rs.getString("BUSI_MSG"));
                apiLog.setLastOperatorId(rs.getString("LAST_OPERATOR_ID"));
                apiLog.setLastOperator(rs.getString("LAST_OPERATOR"));
                apiLog.setLastUpdateTime(rs.getString("LAST_UPDATE_TIME"));

                // 插入mongodb
                mongodbDAO.save(apiLog);
                i++;
                LOG.debug("save count: {} ", i);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public List<ApiLogDomain> listApiLog(){
        Connection conn = JDBCOperation.getConn();
        LOG.debug(sql);
        List<ApiLogDomain> list = new ArrayList<ApiLogDomain>();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            int i = 0;
            while (rs.next()){
                ApiLogDomain apiLog = new ApiLogDomain();
                apiLog.setLogId(rs.getString("LOG_ID"));
                apiLog.setType(rs.getString("TYPE"));
                apiLog.setTxCode(rs.getString("TX_CODE"));
                apiLog.setDockingSys(rs.getString("DOCKING_SYS"));
                apiLog.setRequestTime(rs.getString("REQUEST_TIME"));
                apiLog.setRequestContent(rs.getString("REQUEST_CONTENT"));
                apiLog.setResponseTime(rs.getString("RESPONSE_TIME"));
                apiLog.setResponseContent(rs.getString("RESPONSE_CONTENT"));
                apiLog.setStatus(rs.getString("STATUS"));
                apiLog.setReference(rs.getString("REFERENCE"));
                apiLog.setRequestSeqNo(rs.getString("REQUEST_SEQ_NO"));
                apiLog.setBusiMsg(rs.getString("BUSI_MSG"));
                apiLog.setLastOperatorId(rs.getString("LAST_OPERATOR_ID"));
                apiLog.setLastOperator(rs.getString("LAST_OPERATOR"));
                apiLog.setLastUpdateTime(rs.getString("LAST_UPDATE_TIME"));
                i++;
                list.add(apiLog);
                if(i>=10){
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

}
